local module = {}
module.name = "Spec Detector"
module.Init = function()
	if not SCDB.modules[module.name] then return end

	local AurasTable = {
		["WARRIOR"] = {
			["Arms"] = { 12294, 46924, 85388	,85730, 12328, 60503, 23694, 30069, 30070, 84584, 84585, 84586, 65156, 52437, 57518, 57519 },
			["Fury"] = { 12966, 12967, 12968, 12292, 12880, 14201, 14202, 85386, 29801, 56112, 85738, 85739, 46916 },
			["Protection"] = { 12975, 12809, 57514, 57516, 87095, 50720, 46946, 46947, 50227, 46968 },
		},
		["ROGUE"] = {
			["Assassination"] = { 14177, 58426, 93068, 79140 },
			["Combat"] = { 13877, 51690, 13750, 84617, 31125 },
			["Subtlety"] = { 36554, 51713, 16511, 51698, 51700, 51701, 45182 },
		},
		["DEATHKNIGHT"] = {
			["Blood"] = { 55233, 49222, 53137, 53138 },
			["Frost"] = { 49203, 51271, 51124, 59052, 55610, 50434, 50435 },
			["Unholy"] = { 49016, 91342, 49194, 65142, 81340 },
		},
		["DRUID"] = {
			["Balance"] = { 50288, 78675, 24858, 61391, 93400, 48391, 93402, 60433, 81191 },
			["Feral"] = { 33878, 33876, 50334, 80951, 61336, 45334, 49376, 24932, 82364, 82365 },
			["Restoration"] = { 18562, 33891, 48438, 17116, 48504, 81094, 81093 },
		},
		["HUNTER"] = {
			["Beast Mastery"] = { 19574, 82692, 53257, 34471, 75447 },
			["Marksmanship"] = { 34490, 35101, 63468, 19506, 53230, 82925, 83559, 88691 },
			["Survival"] = { 53301, 3674, 19386, 19306, 56453, 53290, 64418, 64419, 64420 },
		},
		["MAGE"] = {
			["Arcane"] = { 12042, 54646, 31589, 12043, 82930, 57529, 57531, 83098 },
			["Fire"] = { 11366, 44457, 31661, 11113, 64851, 64989, 48108 },
			["Frost"] = { 44572, 11426, 12472, 44544, 57761, 55080, 83073 },
		},
		["PALADIN"] = {
			["Holy"] = { 31821, 53563, 31842, 85509, 53672, 88819, 85496, 85497, 20050 },
			["Protection"] = { 31935, 63529, 31850, 70940, 20178, 26017, 20925, 88063, 85433 },
			["Retribution"] = {	85696, 20066, 59578, 87173 },
		},
		["PRIEST"] = {
			["Discipline"] = { 33206, 89485, 10060, 59888, 47753, 54704, 45241, 47930 },
			["Holy"] = { 88625, 47788, 14751, 724, 63731, 64128, 33143 },
			["Shadow"] = { 15407, 47585, 64044, 34914, 15487, 15286, 15473, 81292, 87193, 87204 },
		},
		["SHAMAN"] = {
			["Elemental"] = { 61882, 16166, 16246, 51466, 51470, 65264, 64695, 77747 },
			["Enchancement"] = { 30823, 17364, 63685, 77661, 30802, 30808, 53817 },
			["Restoration"] = { 974, 61295, 16188, 16177, 16236, 53390 },
		},
		["WARLOCK"] = {
			["Affliction"] = { 30108, 48181, 18223, 60946, 60947, 64368, 32386 },
			["Demonology"] = { 47241, 71162, 93974, 93987, 93975, 93986, 63165, 53646 },
			["Destruction"] = { 80240, 91711, 30283, 29341, 18118, 54276, 85421, 34936, 54371, 54372, 54370, 54375, 54374, 54373, 47283 },
		},
	}

	local SpellsTable = {
		["WARRIOR"] = {
			["Arms"] = { 12294, 46924, 85388, 85730, 12328 },
			["Protection"] = { 23922, 46968, 20243, 12809, 12975, 50720 },
			["Fury"] = { 23881, 60970, 85288, 12292 },
		},
		["ROGUE"] = {
			["Assassination"] = { 1329, 79140, 14177 },
			["Combat"] = { 13877, 51690, 13750, 84617 },
			["Subtlety"] = { 36554, 51713, 14185, 14183, 16511 },
		},
		["DEATHKNIGHT"] = {
			["Blood"] = { 55050, 49028, 48982, 55233, 49222 },
			["Frost"] = { 49143, 49184, 49203, 51271 },
			["Unholy"] = { 55090, 49206, 51052, 63560, 49016 },
		},
		["DRUID"] = {
			["Balance"] = { 78674, 50288, 33831, 78675, 24858, 50516, 93402 },
			["Feral"] = { 33878, 33876, 50334, 80313, 61336, 16979, 49376 },
			["Restoration"] = { 18562, 33891, 48438, 17116 },
		},
		["HUNTER"] = {
			["Beast Mastery"] = { 19577, 19574, 82726, 82692 },
			["Marksmanship"] = { 19434, 53209, 23989, 34490 },
			["Survival"] = { 53301, 3674, 19386, 19306 },
		},
		["MAGE"] = {
			["Arcane"] = { 44425, 12042, 54646, 31589, 12043 },
			["Fire"] = { 11366, 44457, 31661, 11129, 11113 },
			["Frost"] = { 31687, 44572, 11426, 11958, 12472 },
		},
		["PALADIN"] = {
			["Holy"] = { 20473, 85222, 31821, 53563, 31842 },
			["Protection"] = { 31935, 31850, 70940, 53600, 53595 },
			["Retribution"] = { 85256, 85696, 20066, 85285, 53385 },
		},
		["PRIEST"] = {
			["Discipline"] = { 47540, 62618, 33206, 89485, 10060 },
			["Holy"] = { 88625, 47788, 34861, 14751, 724 },
			["Shadow"] = { 15407, 47585, 64044, 34914, 15487, 15286, 15473 },
		},
		["SHAMAN"] = {
			["Elemental"] = { 51490, 61882, 16166 },
			["Enchancement"] = { 60103, 51533, 30823, 17364 },
			["Restoration"] = { 974, 61295, 16190, 16188 },
		},
		["WARLOCK"] = {
			["Affliction"] = { 30108, 48181, 86121, 18223 },
			["Demonology"] = { 30146, 47241, 71521, 47193 },
			["Destruction"] = { 17962, 50796, 80240, 91711, 30283, 17877 },
		},
	}

	local TalentIcons = {
		["DEATHKNIGHT"] = {["Blood"] = "Spell_Shadow_BloodBoil", ["Frost"] = "Spell_Frost_FrostNova", ["Unholy"] = "Spell_Shadow_ShadeTrueSight"},
		["DRUID"] = {["Balance"] = "Spell_Nature_Lightning", ["Feral"] = "Ability_Racial_BearForm", ["Restoration"] = "Spell_Nature_HealingTouch"},
		["HUNTER"] = {["Beast Mastery"] = "Ability_Hunter_BeastTaming", ["Marksmanship"] = "Ability_Marksmanship", ["Survival"] = "Ability_Hunter_SwiftStrike"},
		["MAGE"] = {["Arcane"] = "Spell_Holy_MagicalSentry", ["Fire"] = "Spell_Fire_FlameBolt", ["Frost"] = "Spell_Frost_FrostBolt02"},
		["PALADIN"] = {["Holy"] = "Spell_Holy_HolyBolt", ["Protection"] = "Spell_Holy_DevotionAura", ["Retribution"] = "Spell_Holy_AuraOfLight"},
		["PRIEST"] = {["Discipline"] = "Spell_Holy_WordFortitude", ["Holy"] = "Spell_Holy_HolyBolt", ["Shadow"] = "Spell_Shadow_ShadowWordPain"},
		["ROGUE"] = {["Assassination"] = "Ability_Rogue_Eviscerate", ["Combat"] = "Ability_BackStab", ["Subtlety"] = "Ability_Stealth"},
		["SHAMAN"] = {["Elemental"] = "Spell_Nature_Lightning", ["Enchancement"] = "Spell_Nature_LightningShield", ["Restoration"] = "Spell_Nature_MagicImmunity"},
		["WARLOCK"] = {["Affliction"] = "Spell_Shadow_DeathCoil", ["Demonology"] = "Spell_Shadow_Metamorphosis", ["Destruction"] = "Spell_Shadow_RainOfFire"},
		["WARRIOR"] = {["Arms"] = "Ability_Rogue_Eviscerate", ["Fury"] = "Ability_Warrior_InnerRage", ["Protection"] = "INV_Shield_06"},
	}

	local function AssignFrames()
		for i,v in pairs({"TargetFrame", "ArenaEnemyFrame1", "ArenaEnemyFrame2", "ArenaEnemyFrame3", "ArenaEnemyFrame4", "ArenaEnemyFrame5"}) do
			if _G[v] then
				local object = _G[v]
				local unit = object.unit
				local class = select(2, UnitClass(unit))

				if object.portrait and unit and class then
					
					object.holder = object.holder or CreateFrame("frame", "$parentHolder", object)
					object.holder:SetSize(26, 26)
					object.holder:SetPoint("BOTTOMLEFT", object.portrait, "BOTTOMLEFT", 0, 0)
					object.holder.tex = object.holder.tex or object.holder:CreateTexture("$parentHolderTexture", "OVERLAY")
					object.holder.tex:SetTexture(nil)
					object.holder.tex:SetAllPoints(object.holder)

					if not UnitIsPlayer(unit) then return end
					
					object.holder:UnregisterAllEvents()
					object.holder:SetScript("OnEvent", nil)
					object.holder:RegisterEvent("UNIT_AURA")
					object.holder:RegisterEvent("UNIT_SPELLCAST_START")
					object.holder:RegisterEvent("PLAYER_TARGET_CHANGED")
					object.holder:SetScript("OnEvent", function(self, event, ...)
						if event == "UNIT_AURA" or event == "PLAYER_TARGET_CHANGED" then
							for index=1, 40 do
								local name, _, _, _, _, _, _, unitCaster = UnitAura(unit, index, "HELPFUL")
								if name ~= nil and unitCaster==unit then
									for spec, data in pairs(AurasTable[class]) do
										for _, id in pairs(data) do
											local spellName = GetSpellInfo(id)
											if name == spellName then
												object.holder.tex:SetTexture("Interface\\Icons\\"..TalentIcons[class][spec])
												object.holder.tex:SetTexCoord(0,1,0,1)
												return
											end
										end
									end
								end
							end

						elseif event == "UNIT_SPELLCAST_START" then
							local unitCaster, name = ...
							if name ~= nil and unitCaster==unit then
								for spec, data in pairs(SpellsTable[class]) do
									for _, id in pairs(data) do
										local spellName = GetSpellInfo(id)
										if name == spellName then
											object.holder.tex:SetTexture("Interface\\Icons\\"..TalentIcons[class][spec])
											object.holder.tex:SetTexCoord(0,1,0,1)
											return
										end
									end
								end
							end
						end
					end)
				end
			end
		end
	end

	local SpecDetector = CreateFrame("frame", "SpellDetector", UIParent)
	SpecDetector:RegisterEvent("ZONE_CHANGE")
	SpecDetector:RegisterEvent("PLAYER_ENTERING_WOLRD")
	SpecDetector:RegisterEvent("PLAYER_TARGET_CHANGED")
	SpecDetector:SetScript("OnEvent", function(self, event)
		AssignFrames()
	end)

end
tinsert(SuperClassic.modules, module) -- finish him!